find_package(PkgConfig)
find_package(GTK2 2.20 REQUIRED gtk)
if(NOT GTK2_FOUND)
	pkg_check_modules(GTK2 REQUIRED gtk+-2.0>=2.20)
endif()
pkg_check_modules(GTHREAD2 REQUIRED gthread-2.0>=2.24)

set(CMAKE_C_FLAGS ${CMAKE_C_FLAGS} "-Wall -DENABLE_NLS")
set(GSMON_SOURCES gsm-marshals.c gsm-model.c gsmon.c gsm-view.c gsm-watcher.c gsm-watcher-source.c notifier.c)

if (NOT CMAKE_BUILD_TYPE STREQUAL Debug)
	set(GSMON_CFLAGS ${GSMON_CFLAGS} "-DNDEBUG")
endif()

if(UNIX)
	option(USE_INTERNAL_NOTIFICATOR "Use internal notificator")
	if(NOT USE_INTERNAL_NOTIFICATOR)
		pkg_check_modules(LIBNOTIFY REQUIRED libnotify)
	endif()
endif()

if(WIN32)
	set(GSMON_LIBRARIES ws2_32 winmm)
	set(USE_INTERNAL_NOTIFICATOR ON)
	if (NOT CMAKE_BUILD_TYPE STREQUAL Debug)
		set(GUI_TYPE WIN32)
	endif()
	
	add_custom_command(
		OUTPUT windres.o
		COMMAND windres
		ARGS --input=windres.rc --output=windres.o
		COMMENT "Building resource file"
	)
	set(GSMON_SOURCES ${GSMON_SOURCES} windres.o)
endif()

if(USE_INTERNAL_NOTIFICATOR)
	set(GSMON_CFLAGS ${GSMON_CFLAGS} "-DGSM_USE_INTERNAL_NOTIFIER")
endif()

add_custom_command(
	OUTPUT gsm-marshals.c
	COMMAND glib-genmarshal ARGS --prefix=gsm_marshal --header marshals.list > gsm-marshals.h
	COMMAND glib-genmarshal ARGS --prefix=gsm_marshal --body marshals.list > gsm-marshals.c
	DEPENDS marshals.list
	COMMENT "Generating marshals"
)

include_directories(${GTK2_INCLUDE_DIRS} ${GTHREAD2_INCLUDE_DIRS} ${LIBNOTIFY_INCLUDE_DIRS})
add_definitions(${GTHREAD2_CFLAGS} ${LIBNOTIFY_CFLAGS} ${GSMON_CFLAGS})
add_executable(gsmon ${GUI_TYPE} ${GSMON_SOURCES})
target_link_libraries(gsmon ${GTK2_LIBRARIES} ${GTHREAD2_LIBRARIES} ${LIBNOTIFY_LIBRARIES} ${GSMON_LIBRARIES})

install(TARGETS gsmon DESTINATION ${CMAKE_INSTALL_PREFIX}/bin)
install(DIRECTORY icons DESTINATION ${CMAKE_INSTALL_PREFIX}/share/gsmon
	PATTERN ".svn" EXCLUDE
	PATTERN "gsmon.ico" EXCLUDE)
install(FILES ui/gsmon.glade DESTINATION ${CMAKE_INSTALL_PREFIX}/share/gsmon/ui)

foreach(lang ${GSMON_LANGUAGES})
	install(
		FILES
			po/${lang}
		DESTINATION
			${CMAKE_INSTALL_PREFIX}/share/locale/${lang}/LC_MESSAGES
		RENAME
			gsmon.mo
	)
endforeach()
